package com.sjy.project.fym.lab.controller;

import java.math.BigDecimal;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.fym.lab.domain.FyLab;
import com.sjy.project.fym.lab.service.IFyLabService;
import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.common.utils.GPSUtil;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 实验室信息Controller
 * 
 * @author ljb
 * @date 2022-05-15
 */
@Controller
@RequestMapping("/fym/lab")
public class FyLabController extends BaseController
{
    private String prefix = "fym/lab";

    @Autowired
    private IFyLabService fyLabService;

    @RequiresPermissions("fym:lab:view")
    @GetMapping()
    public String lab()
    {
        return prefix + "/lab";
    }

    /**
     * 查询实验室信息列表
     */
    @RequiresPermissions("fym:lab:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FyLab fyLab)
    {
        startPage();
        List<FyLab> list = fyLabService.selectFyLabList(fyLab);
        return getDataTable(list);
    }
    
    @RequiresPermissions("fym:ljb:import")
    @Log(title = "实验室信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FyLab> util = new ExcelUtil<FyLab>(FyLab.class);
        List<FyLab> list = util.importExcel(file.getInputStream());
        String message = fyLabService.importLab(list, updateSupport);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("fym:ljb:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<FyLab> util = new ExcelUtil<FyLab>(FyLab.class);
        return util.importTemplateExcel("防疫点数据");
    }

    /**
     * 导出实验室信息列表
     */
    @RequiresPermissions("fym:lab:export")
    @Log(title = "实验室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FyLab fyLab)
    {
        List<FyLab> list = fyLabService.selectFyLabList(fyLab);
        ExcelUtil<FyLab> util = new ExcelUtil<FyLab>(FyLab.class);
        return util.exportExcel(list, "实验室信息数据");
    }

    /**
     * 新增实验室信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存实验室信息
     */
    @RequiresPermissions("fym:lab:add")
    @Log(title = "实验室信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FyLab fyLab)
    {
    	//更新lngSjy latSjy 到 lng lat
//        double[] lnglat = GPSUtil.bd09_To_gps84(new Double(fyLab.getLatSjy()+""), new Double(fyLab.getLngSjy()+""));
//        fyLab.setLng(new BigDecimal(lnglat[1]));
//        fyLab.setLat(new BigDecimal(lnglat[0]));
        
        return toAjax(fyLabService.insertFyLab(fyLab));
    }

    /**
     * 修改实验室信息
     */
    @RequiresPermissions("fym:lab:edit")
    @GetMapping("/edit/{labId}")
    public String edit(@PathVariable("labId") Long labId, ModelMap mmap)
    {
        FyLab fyLab = fyLabService.selectFyLabByLabId(labId);
        mmap.put("fyLab", fyLab);
        return prefix + "/edit";
    }

    /**
     * 修改保存实验室信息
     */
    @RequiresPermissions("fym:lab:edit")
    @Log(title = "实验室信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FyLab fyLab)
    {
    	//更新lngSjy latSjy 到 lng lat
//        double[] lnglat = GPSUtil.bd09_To_gps84(new Double(fyLab.getLatSjy()+""), new Double(fyLab.getLngSjy()+""));
//        fyLab.setLng(new BigDecimal(lnglat[1]));
//        fyLab.setLat(new BigDecimal(lnglat[0]));
        
        return toAjax(fyLabService.updateFyLab(fyLab));
    }

    /**
     * 删除实验室信息
     */
    @RequiresPermissions("fym:lab:remove")
    @Log(title = "实验室信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fyLabService.deleteFyLabByLabIds(ids));
    }
}
